// 音效管理

import { ecs } from '../framework/assets/libs/ecs/ECS';
import { AudioClickComponent, AudioSwapComponent, AudioMatchComponent, AudioEliminateComponent } from './utilsComponent';
import {oops} from "../framework/assets/core/Oops";

export class AudioSystem extends ecs.ComblockSystem implements ecs.IEntityEnterSystem {
    entityEnter(entity: ecs.Entity): void {
        if (entity.has(AudioClickComponent)) {
            // 点击
            entity.remove(AudioClickComponent);
            oops.audio.playEffect('audio/click.bubble');
        }
        if (entity.has(AudioSwapComponent)) {
            // 交换
            entity.remove(AudioSwapComponent);
            oops.audio.playEffect('audio/swap');
        }
        if (entity.has(AudioMatchComponent)) {
            let step = entity.get(AudioMatchComponent).step;
            entity.remove(AudioMatchComponent);
            // 最多 11 轮
            step = Math.min(step, 10);
            if (step < 2) {
                return;
            }
            if (step % 2 === 0) {
                // 偶数
                step += 1;
            }
            oops.audio.playEffect('audio/contnuousMatch'+step);
        }
        if (entity.has(AudioEliminateComponent)) {
            let step = entity.get(AudioEliminateComponent).step;
            // 每次消除的时候都要播
            entity.remove(AudioEliminateComponent);
            if (step < 1) {
                step = 1;
            }
            if (step >= 8) {
                // 最多只有8个声音
                step = 8;
            }
            oops.audio.playEffect('audio/eliminate' + step);
        }
    }

    filter(): ecs.IMatcher {
        return ecs.anyOf(AudioClickComponent, AudioSwapComponent, AudioMatchComponent, AudioEliminateComponent);
    }
}
